package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApBehaviorEntryMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

@Service
public class ApBehaviorEntryServiceImpl extends ServiceImpl<ApBehaviorEntryMapper,ApBehaviorEntry> implements ApBehaviorEntryService {

    /**
     * 根据用户id或者设备id查询用户行为实体表
     */
    @Override
    public ApBehaviorEntry findByUserIdOrEquipmentId(Integer userId, Integer equipmentId) {
        //校验参数
        if(userId==null&&equipmentId==null){
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //用户id不为空根据用户id查询行为实体
        if(userId!=null){
            return getOne(Wrappers.<ApBehaviorEntry>lambdaQuery().eq(ApBehaviorEntry::getType, 1)
                    .eq(ApBehaviorEntry::getEntryId, userId));
        }
        if(equipmentId!=null&&equipmentId!=0){
            return getOne(Wrappers.<ApBehaviorEntry>lambdaQuery().eq(ApBehaviorEntry::getType, 0)
                    .eq(ApBehaviorEntry::getEntryId, equipmentId));
        }
        return null;
    }
}
